package bman.tools;

/**
 * This class will keep a amount of logs specified in the by the setBufferSize
 * method.
 * 
 * The default size is 2000000 or 2MB 
 * @author MrJacky
 *
 */
public class LogBuffer
{
	StringBuffer sb1 = new StringBuffer();
	StringBuffer sb2 = new StringBuffer();
	StringBuffer currentBuffer = sb1;
	int length;
	
	public LogBuffer()
	{
		length = 2000000/2;
	}
	/**
	 * @param size the number of characters that this buffer will hold
	 */
	public void setBufferSize(int size)
	{
		length = size/2;
	}
	
	int size;
	
	/**
	 * Adds the passed sting parameter to the log bufffer.
	 * 
	 * <h3>Performance Information</h3>
	 * <p>
	 * <h4>Hardware Information</h4>
	 * Processor: Intel Core 2 Duo E6600, Core Speed 2.4GHz<br>
	 * Memory: DDR2 2048MB, 400MHz<br>
	 * 
	 * <h4>Single Thread Test</h4>
 	 * Load 100MB of data several times and get the average
	 * <br>-----------------------------
	 * <br>Samples  : 10
     * <br>Ave. time: 2472.0 ms
     * <br>-----------------------------
	 * 
	 * <h4>Multi Thread Test</h4>
	 * Run concurrent threads that each logs a total of
	 * 100MB of data to the same instance of LogBuffer
	 * <p>
	 * <br>-----------------------------
	 * <br>Threads  : 10
     * <br>Ave. time: 59428.0 ms
     * <br>-----------------------------
	 * @param s the string desired to be logged.
	 */
	public void log(String s)
	{
		log2(s);
	}
	
	/**
	 * <h3>Performance Test</h3>
	 * <p>
	 * <h4>Hardware Information</h4>
	 * Processor: Intel Core 2 Duo E6600, Core Speed 2.4GHz<br>
	 * Memory: DDR2 2048MB, 400MHz<br>
	 * 
	 * <h4>Single Thread Test</h4>
 	 * Load 100MB of data several times and get the average
	 * <br>-----------------------------
	 * <br>Samples  : 10
     * <br>Ave. time: 4391.0 ms
     * <br>-----------------------------
	 * 
	 * <h4>Multi Thread Test</h4>
	 * Run concurrent threads that each logs a total of
	 * 100MB of data to the same instance of LogBuffer
	 * <p>
	 * <br>-----------------------------
	 * <br>Threads  : 10
     * <br>Ave. time: 75867.0 ms
     * <br>-----------------------------
     * 
	 * @param s
	 */
	private void log1(String s)
	{
		if(size >= length)
		{
			if(currentBuffer == sb1) 
			{
				/*
				 * switch to sb2
				 */
				sb2 = new StringBuffer();
				currentBuffer = sb2;
			}
			else 
			{
				/*
				 * currentBuffer must be sb2 
				 * so switch to sb1
				 */
				sb1 = new StringBuffer();
				currentBuffer = sb1;
			}
			size = 0;
		}
		
		/*
		 * I arrived at 3 by trial and error.
		 * It seems the terminator \n is 3 chars.
		 * I'm not sure of this though.
		 */
		size += s.length()+3;
		currentBuffer.append(s+"\n");
	}
	
	/**
	 * <h3>Performance Test</h3>
	 * <p>
	 * <h4>Hardware Information</h4>
	 * Processor: Intel Core 2 Duo E6600, Core Speed 2.4GHz<br>
	 * Memory: DDR2 2048MB, 400MHz<br>
	 * 
	 * <h4>Single Thread Test</h4>
 	 * Load 100MB of data several times and get the average
	 * <br>-----------------------------
	 * <br>Samples  : 10
     * <br>Ave. time: 2472.0 ms
     * <br>-----------------------------
	 * 
	 * <h4>Multi Thread Test</h4>
	 * Run concurrent threads that each logs a total of
	 * 100MB of data to the same instance of LogBuffer
	 * <p>
	 * <br>-----------------------------
	 * <br>Threads  : 10
     * <br>Ave. time: 59428.0 ms
     * <br>-----------------------------
	 */
	private void log2(String s)
	{
		if(size >= length)
		{
			if(currentBuffer == sb1) 
			{
				/*
				 * switch to sb2
				 */
				sb2.setLength(0);
				currentBuffer = sb2;
			}
			else 
			{
				/*
				 * currentBuffer must be sb2 
				 * so switch to sb1
				 */
				sb1.setLength(0);
				currentBuffer = sb1;
			}
			size = 0;
		}
		
		/*
		 * I arrived at 3 by trial and error.
		 * It seems the terminator \n is 3 chars.
		 * I'm not sure of this though.
		 */
		size += s.length()+3;
		currentBuffer.append(s+"\n");
	}
	
	/**
	 * <h3>Performance Test</h3>
	 * <p>
	 * <h4>Hardware Information</h4>
	 * Processor: Intel Core 2 Duo E6600, Core Speed 2.4GHz<br>
	 * Memory: DDR2 2048MB, 400MHz<br>
	 * 
	 * <h4>Single Thread Test</h4>
 	 * Load 100MB of data several times and get the average
	 * <br>-----------------------------
	 * <br>Samples  : 10
     * <br>Ave. time: 2882.0 ms
     * <br>-----------------------------
	 * 
	 * <h4>Multi Thread Test</h4>
	 * Run concurrent threads that each logs a total of
	 * 100MB of data to the same instance of LogBuffer
	 * <p>
	 * <br>-----------------------------
	 * <br>Threads  : 10
     * <br>Ave. time: 103351.0 ms
     * <br>-----------------------------
     * 
	 * @param s
	 */
	private void log3(String s)
	{
		if(currentBuffer.length() >= length)
		{
			if(currentBuffer == sb1) 
			{
				/*
				 * switch to sb2
				 */
				sb2.setLength(0);
				currentBuffer = sb2;
			}
			else 
			{
				/*
				 * currentBuffer must be sb2 
				 * so switch to sb1
				 */
				sb1.setLength(0);
				currentBuffer = sb1;
			}
		}
		currentBuffer.append(s+"\n");
	}
	
	/**
	 * 
	 * @return returns the contents of this buffer
	 */
	public String getContents()
	{
		if(currentBuffer == sb1)
		{
			return currentBuffer.toString()+sb2.toString();
		}
		else
		{
			return currentBuffer.toString()+sb1.toString();
		}
	}
}
